#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tornado.web import RequestHandler
import pymongo
import re
import render
import codecs
from _base import BaseHandler
import datetime
import sys
sys.path.insert(0,'/home/liuyue/svn/wan/')
sys.path.insert(0,'/home/leeray/workspaces/Python/wanalytics/')
sys.path.insert(0,'/opt/web/')
from Tools import getPid
from Tools import connectMySQL 
import json
import json_ly
from util import cache, settings
import _clientCache as clientcache
import logging


class PaikeClient(BaseHandler):
    def get(self): 
        os_json = clientcache.getOS()
        
        pidtype_json = clientcache.getPidType()
        
        _time = self.get_argument('staDate', '2011/11/13').strip()
        _pd = self.get_argument('pd','').strip()
        
        try:
            st_time = datetime.datetime.strptime(_time, '%Y/%m/%d')
        except :
            self.send_error(400, desc='invalid argument')
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            #self.finish()
            return
        
        if not _pd:
            ed_time = datetime.datetime.now()
            delta = ed_time - st_time 
            _pd =  abs(delta.days)
        _pd = int(_pd)
        et_time = st_time + datetime.timedelta(days=_pd)
        st_datestr = st_time.strftime('%Y%m%d')
        et_datestr = et_time.strftime('%Y%m%d')
        
        all_data = []
        results = {}
        os_data = []
        
        db = connectMySQL.connMySQL_stat()
        c=db.cursor()
        #sql = "SELECT date, os, type, pv, uv, vv  FROM `view_day_mainAnalytics` WHERE date>= '{0}' and date < '{1}'".format(st_datestr, et_datestr)
        sql = "SELECT date, os_id, device_type, sum(pv), sum(uv), sum(vv), sum(newuser), sum(upload), sum(register)  FROM `stat_day_paike` WHERE date>= '{0}' and date < '{1}' and business_type=4 and pid_stat=1 group by date, os_id, device_type".format(st_datestr, et_datestr)
        logging.info(sql)
        c.execute(sql)  
        res= c.fetchall()
        _tmpdate = ''
        for x in res:
            date = str(x[0])
            os = os_json.get(int(x[1])) or 'null'
            #type1 = pidtype_json.get(int(x[2])) or 'null'
            type1 = settings.device_type.get(str(x[2])) or 'unkown'
            pv = int(x[3])
            uv = int(x[4])
            vv = int(x[5])
            nu = int(x[6])
            su = int(x[7])
            nr = int(x[8])
            
            if _tmpdate and _tmpdate != date:
                results['data'] = os_data
                results['year'] = _tmpdate[:4]
                results['month'] = _tmpdate[4:6]
                results['day'] = _tmpdate[6:]
                all_data.append(results)
                results = {}
                os_data = []
            
            os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu})
            _tmpdate = date
        
        if len(os_data) > 0 :
            results['data'] = os_data
            results['year'] = _tmpdate[:4]
            results['month'] = _tmpdate[4:6]
            results['day'] = _tmpdate[6:]
            all_data.append(results)
            results = {}
            os_data = []
        
        c.close()
        db.close()
        
        self.write({"status":"success", "items":all_data})
        self.finish()
        
class MainPaikeAnalytics(BaseHandler):
    def get(self): 
        os_json = clientcache.getOS()
        openmix = 0
        table_name = "stat_day_paike"
        
        pidtype_json = clientcache.getPidType()
        
        _time = self.get_argument('staDate', '2011/11/13').strip()
        _pd = self.get_argument('pd','').strip()
        
        _dt = self.get_argument('dt','no').strip()
        _os_id = self.get_argument('os','no').strip()
        _pm = self.get_argument('pm','no').strip()
        
        ossql = ""
        
        if (_os_id == "no") and (_dt == "no") and (_pm == "no") :
            ossql = "   "
        elif (_os_id != "no") and (_dt == "no") and (_pm == "no") :
            ossql = " ,device_type  "
        elif (_os_id == "no") and (_dt != "no") and (_pm == "no") :
            ossql = " ,device_type  "
        elif (_os_id == "no") and (_dt == "no") and (_pm != "no") :
            ossql = " ,partner_mode  "
        elif (_os_id != "no") and (_dt != "no") and (_pm != "no") :
            ossql = " ,device_type,partner_mode  "
        elif (_os_id != "no") and (_dt != "no") and (_pm == "no") :
            ossql = " ,device_type,partner_mode  "                 
        elif (_os_id != "no") and (_dt == "no") and (_pm != "no") :
            ossql = " ,device_type,partner_mode  "
        elif (_os_id == "no") and (_dt != "no") and (_pm != "no") :
            ossql = " ,device_type,partner_mode  "
        
        
        
        try:
            st_time = datetime.datetime.strptime(_time, '%Y/%m/%d')
        except :
            self.send_error(400, desc='invalid argument')
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            #self.finish()
            return
        
        if not _pd:
            ed_time = datetime.datetime.now()
            delta = ed_time - st_time 
            _pd =  abs(delta.days)
        _pd = int(_pd)
        et_time = st_time + datetime.timedelta(days=_pd)
        st_datestr = st_time.strftime('%Y%m%d')
        et_datestr = et_time.strftime('%Y%m%d')
        
        all_data = []
        results = {}
        os_data = []
        
        db = connectMySQL.connMySQL_stat()
        c=db.cursor()
        #sql = "SELECT date, os, type, pv, uv, vv  FROM `view_day_mainAnalytics` WHERE date>= '{0}' and date < '{1}'".format(st_datestr, et_datestr)
        #if _os_id == "no":
        sql = "SELECT date, os_id, device_type, sum(pv), sum(uv), sum(vv), sum(newuser), sum(upload), sum(register) , partner_mode,sum(before_duration),sum(before_times),sum(duration),sum(complete),sum(play_load_duration),sum(play_load_times),sum(play_hdsd_times),sum(play_hdsd_duration),sum(play_sdhd_times),sum(play_sdhd_duration),apiresp,server  FROM `stat_day_paike` WHERE date>= '{0}' and date < '{1}' and business_type=4 and pid_stat=1 and os_id in (52,61) group by date, os_id {2} ".format(st_datestr, et_datestr,ossql)
        logging.info(sql)
        print sql
        c.execute(sql)  
        res= c.fetchall()
        _tmpdate = ''
        
        
        
        for x in res:
            date = str(x[0])
            os = os_json.get(int(x[1])) or 'null'
            #type1 = pidtype_json.get(int(x[2])) or 'null'
            type1 = settings.device_type.get(str(x[2])) or 'unkown'
            pv = int(x[3])
            uv = int(x[4])
            vv = int(x[5])
            nu = int(x[6])
            su = int(x[7])
            nr = int(x[8])
            pm = int(x[9])
            before_duration = int(x[10])
            before_times = int(x[11])
            duration = int(x[12])
            complete = int(x[13])
            play_load_duration = int(x[14])
            play_load_times = int(x[15])
            play_hdsd_times = int(x[16])
            play_hdsd_duration = int(x[17])
            play_sdhd_times = int(x[18])
            play_sdhd_duration = int(x[19])
            
            
            if _tmpdate and _tmpdate != date:
                results['data'] = os_data
                results['year'] = _tmpdate[:4]
                results['month'] = _tmpdate[4:6]
                results['day'] = _tmpdate[6:]
                all_data.append(results)
                results = {}
                os_data = []
            
            
            
            if _os_id == "ios":
                _os_id = "iOS"
                _os_id_sp = "52"
            if _os_id == "android":
                _os_id = "Android"
                _os_id_sp = "61"
            if _os_id == "wp7":
                _os_id = "WP7"
                _os_id_sp = "59"
                
            if _dt == "pad":
                _dt = "Pad"
            if _dt == "phone":
                _dt = "Phone"
                
            if _pm == "qudao":
                _pm = 2
            if _pm == "changshang":
                _pm = 1
            
            if pm == 8:
                pm = 2
            
                
            pm_cn = ""
            if pm == 1:
                pm_cn = u"厂商内置"
            elif pm == 2:
                pm_cn = u"渠道推广"
                
            
            
            
            
            if (_os_id == "no") and (_dt == "no") and (_pm == "no") :
                play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],"no","no")
                os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})    
                        
            elif (_os_id != "no") and (_dt == "no") and (_pm == "no") :
                if _os_id == os:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],"no")
                    os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})            
            elif (_os_id == "no") and (_dt != "no") and (_pm == "no") :
                        
                if _dt == type1:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],"no")
                    os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})            
                        
            elif (_os_id == "no") and (_dt == "no") and (_pm != "no") :
                if _pm == pm:
                    if _pm != 2:
                        play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],"no",pm)
                        os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
                'complete' : complete,
                'play_load_duration' : play_load_duration,
                'play_load_times' : play_load_times,
                'play_hdsd_times' : play_hdsd_times,
                'play_hdsd_duration' : play_hdsd_duration,
                'play_sdhd_times' : play_sdhd_times,
                'play_sdhd_duration' : play_sdhd_duration,
                'play_times' : play_times,
                'play_resptime' : play_resptime,
            'draw_resptime' : draw_resptime,
            'draw_times' : draw_times,
            'getVideoDetail_resptime' : getVideoDetail_resptime,
            'getVideoDetail_times' : getVideoDetail_times,})            
                    
                    else:
                        _datetime = x[0]
                        strc = [{'name':'Android','id':'61'},{'name':'iOS','id':'52'}]
                        for j in xrange(len(strc)):
                                    
                            sql = "SELECT date, os_id, device_type, sum(pv), sum(uv), sum(vv), sum(newuser), sum(upload), sum(register) , partner_mode,sum(before_duration),sum(before_times),sum(duration),sum(complete),sum(play_load_duration),sum(play_load_times),sum(play_hdsd_times),sum(play_hdsd_duration),sum(play_sdhd_times),sum(play_sdhd_duration),apiresp,server  FROM `stat_day_paike` WHERE date = %s and business_type=4 and partner_mode in (2,8) and pid_stat=1 and os_id = %s " % (_datetime,strc[j]['id'])
                            c.execute(sql)
                            print sql  
                            _res = c.fetchone()
                            print _res
                            _os_data_string = str(os_data)
                            if _res[0] == None:
                                if _os_data_string.find(strc[j]['name']) == -1:
                                    os_data.insert(0,{'os':strc[j]['name'],'pm':'渠道推广'})
                            else:
                                
                                
                                pv = int(_res[3])
                                uv = int(_res[4])
                                vv = int(_res[5])
                                nu = int(_res[6])
                                su = int(_res[7])
                                nr = int(_res[8])
                                pm = int(_res[9])
                                before_duration = int(_res[10])
                                before_times = int(_res[11])
                                duration = int(_res[12])
                                complete = int(_res[13])
                                play_load_duration = int(_res[14])
                                play_load_times = int(_res[15])
                                play_hdsd_times = int(_res[16])
                                play_hdsd_duration = int(_res[17])
                                play_sdhd_times = int(_res[18])
                                play_sdhd_duration = int(_res[19])
                                
                                
                                play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(_datetime,table_name,st_datestr,et_datestr,_res[1],"no",2)
                                        
                                        
                                if _os_data_string.find(strc[j]['name']) == -1:
                                        
                                    os_data.insert(0,{'date':_datetime,'os':strc[j]['name'],'pms':_res[7],'pv':pv, 'uv':uv, 'vv':vv, 'nu': nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,'complete' : complete,'play_load_duration' : play_load_duration,'play_load_times' : play_load_times,'play_hdsd_times' : play_hdsd_times,'play_hdsd_duration' : play_hdsd_duration,'play_sdhd_times' : play_sdhd_times,'play_sdhd_duration' : play_sdhd_duration,'play_times' : play_times,'play_resptime' : play_resptime,'draw_resptime' : draw_resptime,'draw_times' : draw_times,'getVideoDetail_resptime' : getVideoDetail_resptime,'getVideoDetail_times' : getVideoDetail_times,})
                       
            elif (_os_id != "no") and (_dt != "no") and (_pm != "no") :
                        
                if _os_id == os and _dt == type1 and _pm == pm:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],pm)
                    if _pm == 2:
                        openmix = 1
                    
                    os_data.insert(0,{'os':os,'pms':x[9],'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})            
                            
                       
            elif (_os_id == "no") and (_dt != "no") and (_pm != "no") :
                if _dt == type1 and _pm == pm:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],pm)     
                    os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})            
                                
                        
            elif (_os_id != "no") and (_dt != "no") and (_pm == "no") :
                if _os_id == os and _dt == type1:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],"no")
                    openmix = 1
                    os_data.insert(0,{'os':os,'pms':x[9],'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})            
                               
            elif (_os_id != "no") and (_dt == "no") and (_pm != "no") :
                if _os_id == os and _pm == pm:
                    
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],pm)
                    
                    if _pm != 2:
                    
                        
                        os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
                'complete' : complete,
                'play_load_duration' : play_load_duration,
                'play_load_times' : play_load_times,
                'play_hdsd_times' : play_hdsd_times,
                'play_hdsd_duration' : play_hdsd_duration,
                'play_sdhd_times' : play_sdhd_times,
                'play_sdhd_duration' : play_sdhd_duration,
                'play_times' : play_times,
                'play_resptime' : play_resptime,
            'draw_resptime' : draw_resptime,
            'draw_times' : draw_times,
            'getVideoDetail_resptime' : getVideoDetail_resptime,
            'getVideoDetail_times' : getVideoDetail_times,})           
            
            
                    else:
                        _datetime = x[0]
                        strc = [{'name':'Phone','id':'1'},{'name':'Pad','id':'2'}]
                        for j in xrange(len(strc)):
                                    
                            sql = "SELECT date, os_id, device_type, sum(pv), sum(uv), sum(vv), sum(newuser), sum(upload), sum(register) , partner_mode,sum(before_duration),sum(before_times),sum(duration),sum(complete),sum(play_load_duration),sum(play_load_times),sum(play_hdsd_times),sum(play_hdsd_duration),sum(play_sdhd_times),sum(play_sdhd_duration),apiresp,server  FROM `stat_day_paike` WHERE date = %s and business_type=4 and partner_mode in (2,8) and pid_stat=1 and device_type = %s and os_id = %s " % (_datetime,strc[j]['id'],_os_id_sp)
                            c.execute(sql)
                            print sql  
                            _res = c.fetchone()
                            print _res
                            _os_data_string = str(os_data)
                            if _res[0] == None:
                                if _os_data_string.find(strc[j]['name']) == -1:
                                    os_data.insert(0,{'type':strc[j]['name'],'pm':'渠道推广'})
                            else:
                                
                                
                                pv = int(_res[3])
                                uv = int(_res[4])
                                vv = int(_res[5])
                                nu = int(_res[6])
                                su = int(_res[7])
                                nr = int(_res[8])
                                pm = int(_res[9])
                                before_duration = int(_res[10])
                                before_times = int(_res[11])
                                duration = int(_res[12])
                                complete = int(_res[13])
                                play_load_duration = int(_res[14])
                                play_load_times = int(_res[15])
                                play_hdsd_times = int(_res[16])
                                play_hdsd_duration = int(_res[17])
                                play_sdhd_times = int(_res[18])
                                play_sdhd_duration = int(_res[19])
                                
                                
                                play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(_datetime,table_name,st_datestr,et_datestr,_res[1],"no",2)
                                        
                                        
                                if _os_data_string.find(strc[j]['name']) == -1:
                                        
                                    os_data.insert(0,{'date':_datetime,'type':strc[j]['name'],'pms':_res[7],'pv':pv, 'uv':uv, 'vv':vv, 'nu': nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,'complete' : complete,'play_load_duration' : play_load_duration,'play_load_times' : play_load_times,'play_hdsd_times' : play_hdsd_times,'play_hdsd_duration' : play_hdsd_duration,'play_sdhd_times' : play_sdhd_times,'play_sdhd_duration' : play_sdhd_duration,'play_times' : play_times,'play_resptime' : play_resptime,'draw_resptime' : draw_resptime,'draw_times' : draw_times,'getVideoDetail_resptime' : getVideoDetail_resptime,'getVideoDetail_times' : getVideoDetail_times,})  
            
            
            
            
            
            
            
            
            
            
            _tmpdate = date
        
        if len(os_data) > 0 :
            results['data'] = os_data
            results['year'] = _tmpdate[:4]
            results['month'] = _tmpdate[4:6]
            results['day'] = _tmpdate[6:]
            all_data.append(results)
            results = {}
            os_data = []
        
        c.close()
        db.close()
        
        
        
        all_data_copy = all_data
        
        
        
        
        
        
        
        
        if openmix == 1:
        
            pms2 = (2,8)
            for item_copy in all_data_copy:
                datas_copy = item_copy.get('data', [])
                tmp_data_copy = []
                del_index = []
                for i in range(len(datas_copy)):
                    if datas_copy[i].get('pms', 0) in pms2:
                        tmp_data_copy.append(datas_copy[i])
                        del_index.append(i)
                
                for i in range(len(del_index)):
                    if i == 0:
                        datas_copy.remove(datas_copy[del_index[i]])
                    else:
                        datas_copy.remove(datas_copy[del_index[i] - 1])
                
                tmp_data_result = {}
                for tmp_data_json in tmp_data_copy:
                    for key,value in tmp_data_json.items():
                        
                        if key not in ('os', 'type', 'pm','pms') :
                            tmp_data_result[key] = tmp_data_result.get(key,0) + value
                        else:
                            if key == 'pm':
                                tmp_data_result['pm'] = value
                            if key == 'os':
                                tmp_data_result['os'] = value
                            if key == 'type':
                                tmp_data_result['type'] = value
                datas_copy.append(tmp_data_result)
        
        
        
        self.write({"status":"success", "items":all_data})
        self.finish()
        
        

class OldMainPaikeAnalytics(BaseHandler):
    def get(self): 
        os_json = clientcache.getOS()
        
        table_name = "stat_day_paike"
        
        pidtype_json = clientcache.getPidType()
        
        _time = self.get_argument('staDate', '2011/11/13').strip()
        _pd = self.get_argument('pd','').strip()
        
        _dt = self.get_argument('dt','no').strip()
        _os_id = self.get_argument('os','no').strip()
        _pm = self.get_argument('pm','no').strip()
        
        ossql = ""
        
        if (_os_id == "no") and (_dt == "no") and (_pm == "no") :
            ossql = "   "
        elif (_os_id != "no") and (_dt == "no") and (_pm == "no") :
            ossql = " ,device_type  "
        elif (_os_id == "no") and (_dt != "no") and (_pm == "no") :
            ossql = " ,device_type  "
        elif (_os_id == "no") and (_dt == "no") and (_pm != "no") :
            ossql = " ,partner_mode  "
        elif (_os_id != "no") and (_dt != "no") and (_pm != "no") :
            ossql = " ,device_type,partner_mode  "
        elif (_os_id != "no") and (_dt != "no") and (_pm == "no") :
            ossql = " ,device_type,partner_mode  "                 
        elif (_os_id != "no") and (_dt == "no") and (_pm != "no") :
            ossql = " ,device_type,partner_mode  "
        elif (_os_id == "no") and (_dt != "no") and (_pm != "no") :
            ossql = " ,device_type,partner_mode  "
        
        
        
        try:
            st_time = datetime.datetime.strptime(_time, '%Y/%m/%d')
        except :
            self.send_error(400, desc='invalid argument')
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            #self.finish()
            return
        
        if not _pd:
            ed_time = datetime.datetime.now()
            delta = ed_time - st_time 
            _pd =  abs(delta.days)
        _pd = int(_pd)
        et_time = st_time + datetime.timedelta(days=_pd)
        st_datestr = st_time.strftime('%Y%m%d')
        et_datestr = et_time.strftime('%Y%m%d')
        
        all_data = []
        results = {}
        os_data = []
        
        db = connectMySQL.connMySQL_stat()
        c=db.cursor()
        #sql = "SELECT date, os, type, pv, uv, vv  FROM `view_day_mainAnalytics` WHERE date>= '{0}' and date < '{1}'".format(st_datestr, et_datestr)
        sql = "SELECT date, os_id, device_type, sum(pv), sum(uv), sum(vv), sum(newuser), sum(upload), sum(register) , partner_mode,sum(before_duration),sum(before_times),sum(duration),sum(complete),sum(play_load_duration),sum(play_load_times),sum(play_hdsd_times),sum(play_hdsd_duration),sum(play_sdhd_times),sum(play_sdhd_duration),apiresp,server  FROM `stat_day_paike` WHERE date>= '{0}' and date < '{1}' and business_type=4 and server != 'api3' and pid_stat=1 group by date, os_id {2} ".format(st_datestr, et_datestr,ossql)
        logging.info(sql)
        print sql
        c.execute(sql)  
        res= c.fetchall()
        _tmpdate = ''
        
        
        
        for x in res:
            date = str(x[0])
            os = os_json.get(int(x[1])) or 'null'
            #type1 = pidtype_json.get(int(x[2])) or 'null'
            type1 = settings.device_type.get(str(x[2])) or 'unkown'
            pv = int(x[3])
            uv = int(x[4])
            vv = int(x[5])
            nu = int(x[6])
            su = int(x[7])
            nr = int(x[8])
            pm = int(x[9])
            before_duration = int(x[10])
            before_times = int(x[11])
            duration = int(x[12])
            complete = int(x[13])
            play_load_duration = int(x[14])
            play_load_times = int(x[15])
            play_hdsd_times = int(x[16])
            play_hdsd_duration = int(x[17])
            play_sdhd_times = int(x[18])
            play_sdhd_duration = int(x[19])
            
            
            if _tmpdate and _tmpdate != date:
                results['data'] = os_data
                results['year'] = _tmpdate[:4]
                results['month'] = _tmpdate[4:6]
                results['day'] = _tmpdate[6:]
                all_data.append(results)
                results = {}
                os_data = []
            
            
            
            if _os_id == "ios":
                _os_id = "iOS"
            if _os_id == "android":
                _os_id = "Android"
            if _os_id == "wp7":
                _os_id = "WP7"
                
            if _dt == "pad":
                _dt = "Pad"
            if _dt == "phone":
                _dt = "Phone"
                
            if _pm == "qudao":
                _pm = 2
            if _pm == "changshang":
                _pm = 1
                
            pm_cn = ""
            if (pm == 1 or pm == 3):
                pm_cn = u"厂商内置"
            elif pm == 2:
                pm_cn = u"渠道推广"
                
            if pm == 3:
                pm = 1
            
            
            
            if (_os_id == "no") and (_dt == "no") and (_pm == "no") :
                play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],"no","no")
                os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})    
                        
            elif (_os_id != "no") and (_dt == "no") and (_pm == "no") :
                if _os_id == os:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],"no")
                    os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})            
            elif (_os_id == "no") and (_dt != "no") and (_pm == "no") :
                        
                if _dt == type1:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],"no")
                    os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})            
                        
            elif (_os_id == "no") and (_dt == "no") and (_pm != "no") :
                if _pm == pm:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],"no",pm)
                    os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})            
                       
            elif (_os_id != "no") and (_dt != "no") and (_pm != "no") :
                        
                if _os_id == os and _dt == type1 and _pm == pm:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],pm)
                    os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})            
                            
                       
            elif (_os_id == "no") and (_dt != "no") and (_pm != "no") :
                if _dt == type1 and _pm == pm:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],pm)     
                    os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})            
                                
                        
            elif (_os_id != "no") and (_dt != "no") and (_pm == "no") :
                if _os_id == os and _dt == type1:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],"no")
                    os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})            
                               
            elif (_os_id != "no") and (_dt == "no") and (_pm != "no") :
                if _os_id == os and _pm == pm:
                    play_resptime,play_times,draw_resptime,draw_times,getVideoDetail_resptime,getVideoDetail_times = clientcache.getJsondata(x[0],table_name,st_datestr,et_datestr,x[1],x[2],pm)
                    os_data.insert(0,{'os':os, 'type':type1, 'pv':pv, 'uv':uv, 'vv':vv, 'nr':nr, 'su':su, 'nu':nu,'pm':pm_cn,'before_duration' : before_duration,'before_times' : before_times,'duration' : duration,
            'complete' : complete,
            'play_load_duration' : play_load_duration,
            'play_load_times' : play_load_times,
            'play_hdsd_times' : play_hdsd_times,
            'play_hdsd_duration' : play_hdsd_duration,
            'play_sdhd_times' : play_sdhd_times,
            'play_sdhd_duration' : play_sdhd_duration,
            'play_times' : play_times,
            'play_resptime' : play_resptime,
        'draw_resptime' : draw_resptime,
        'draw_times' : draw_times,
        'getVideoDetail_resptime' : getVideoDetail_resptime,
        'getVideoDetail_times' : getVideoDetail_times,})           
            
            
            
            
            
            
            
            
            
            
            _tmpdate = date
        
        if len(os_data) > 0 :
            results['data'] = os_data
            results['year'] = _tmpdate[:4]
            results['month'] = _tmpdate[4:6]
            results['day'] = _tmpdate[6:]
            all_data.append(results)
            results = {}
            os_data = []
        
        c.close()
        db.close()
        
        self.write({"status":"success", "items":all_data})
        self.finish()



class PaikeTotalUser(BaseHandler):
    def get(self): 
        os_json = clientcache.getOS()
        pidtype_json = clientcache.getPidType()
        
        _time = self.get_argument('endDate', '').strip()
        try:
            ed_datestr = None
            if _time :
                ed_time = datetime.datetime.strptime(_time, '%Y/%m/%d')
                ed_datestr = ed_time.strftime('%Y%m%d')
            else :
                ed_time = None
        except :
            self.send_error(400, desc='invalid argument')
            #self.write({"status": "failed", "code": 400,  "desc": "invalid argument"})
            #self.finish()
            return
        
        os_data = []

        db = connectMySQL.connMySQL_stat()
        c=db.cursor()
        if not ed_datestr :
            sql = '''select tmp_a.date, tmp_a.os_id, tmp_a.device_type, max(tmp_a.alluser)  from 
            (SELECT date, os_id, device_type, sum(alluser) alluser FROM `stat_day_paike` where date<=(select max(b.date) from `stat_day_paike` b) and business_type=4 and pid_stat=1 group by date, os_id, device_type order by date desc) as tmp_a 
            group by tmp_a.os_id, tmp_a.device_type'''
        else :
            sql = '''select tmp_a.date, tmp_a.os_id, tmp_a.device_type, max(tmp_a.alluser)  from 
            (SELECT date, os_id, device_type, sum(alluser) alluser FROM `stat_day_paike` where date<={0}  and business_type=4 and pid_stat=1 group by date, os_id, device_type order by date desc) as tmp_a 
            group by tmp_a.os_id, tmp_a.device_type'''.format(ed_datestr)
        #sql = "SELECT date, os_id, device_type, alluser  FROM `stat_day_paike` group by os_id order by alluser desc"
        logging.info(sql)
        c.execute(sql)  
        res= c.fetchall()
        for x in res:
            os = os_json.get(int(x[1])) or 'null'
            #type1 = pidtype_json.get(int(x[2])) or 'null'
            type1 = settings.device_type.get(str(x[2])) or 'unkown'
            tu = int(x[3])
            os_data.insert(0,{'os':os, 'type':type1, 'tu':tu})

        c.close()
        db.close()

        self.write({"status":"success", "items":os_data})
        self.finish()
